#include <cstring>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
using namespace std;
//compile:   g++ readast.cpp -o readast
//argv:   ./readast /home/song/Desktop/a/traverse/build/AST/adapter.txt

char **search_classname(const char *filepath); //遍历解析所有的文件AST-classname
char **search_classrange(const char *filepath);  //遍历解析所有的文件AST-classrange
char **search_baseclass(const char *filepath);  //遍历解析所有的文件AST-baseclass

typedef struct range{
    int startline;
    int startcolumn;
    int endline;
    int endcolumn;
};

typedef struct method{
    struct range rangeinfo;
    char methodname[20];
    char operation[20][20];
   
};


typedef struct classinfo{
    int line;
    struct range rangeinfo;
    char classname[20];
    char baseclass[20];
    char property[20][20];
    struct method me[10];
};

typedef struct relationship{
    int relation;   //2,3,5,7,11
    char classname1[20];
    char classname2[20];
};


int main(int argc, char *argv[]) {
  int i,j;
  char *filepath = argv[1];
  const char *filepath2 = "/home/song/Desktop/a/traverse/build/AST/adapter.txt";
  char **classname=search_classname(filepath); //遍历解析所有的文件AST-classname
  //char **search_classrange(const char *filepath)  //遍历解析所有的文件AST-classrange
  char **baseclass=search_baseclass(filepath);  //遍历解析所有的文件AST-classrange

    for(i=0;baseclass[i][0]!=NULL;i++)
  {
    cout<<"baseclass: "<<baseclass[i]<<endl;
  }
    
  for(i=0;classname[i][0]!=NULL;i++)
  {
    cout<<"******"<<classname[i]<<endl;
  }
  

  
  struct range range1={1,5,2,3};
  char *classname1="father";
  char *baseclass1="grandpa";
  char *methodname1="calage";
  char operation1[10][50]={{"callcon"},{"declvar"}};
  char property1[10][50]={{"age"},{"name"},{"phone"}};
  struct classinfo cinfo1[50];
  struct method me1[10];
  cinfo1[0].rangeinfo=range1;
  cinfo1[0].me[0].rangeinfo=range1;
  strcpy(cinfo1[0].classname,classname1);
  strcpy(cinfo1[0].baseclass,baseclass1);
  strcpy(cinfo1[0].me[0].methodname,methodname1);
    for(i=0;i<2;i++)
  {
      strcpy(cinfo1[0].me[0].operation[i],operation1[i]);
  }
  for(i=0;i<3;i++)
  {
      strcpy(cinfo1[0].property[i],property1[i]);
  }
  
  cout<<cinfo1[0].rangeinfo.startline<<endl;
  cout<<cinfo1[0].me[0].rangeinfo.endcolumn<<endl;
  cout<<cinfo1[0].classname<<endl;
  cout<<cinfo1[0].baseclass<<endl;
  cout<<cinfo1[0].property[0]<<endl;
  cout<<cinfo1[0].property[1]<<endl;
  cout<<cinfo1[0].property[2]<<endl;
  cout<<cinfo1[0].me[0].methodname<<endl; 
  cout<<cinfo1[0].me[0].operation[0]<<endl; 
  cout<<cinfo1[0].me[0].operation[1]<<endl; 

  return 0;
  }
  
  
char **search_classname(const char *filepath)  //遍历解析所有的文件AST-classname
{
        char* code=new char[200];
        strcpy(code,"/home/song/Desktop/a/traverse/build/AST/adapter.txt");
     int i,j,k,l;    
     int linenum=1;
     int ranges[20][4];
     std::ifstream fin(filepath);
     std::string line;
     char **classnames;
     classnames=(char **)malloc(20*sizeof(char *));
	for (i=0;i<20;i++)
		classnames[i]=(char *)malloc(50*sizeof(char));
     int flag1=0,flag2=0;
     while (getline(fin, line)) {
     
     const char *cstr = line.c_str();
     char *pc = new char[100];
     strcpy(pc, cstr);
     int i,l; 
        if (pc[0] == '4'&& pc[1]==',') {
        cout<<"line:  "<<linenum<<endl;
          for(i=2;pc[i]!=',';i++)
          {
              classnames[flag1][i-2]=pc[i];  //将“,”后边的类名称存入字符数组
          }
          
         flag1++;   //flag1是类名的个数，循环一次加一次
         flag2=i+1; //flag2是标记“,”位置的，逐步向后移
          
         for(j=0;j<4;j++)
         {
            char *range=new char[10];
             l=0;
             while(pc[flag2]!=',')
            {
               range[l]=pc[flag2];
               flag2++;
               l++;
              // cout<<"***"<<range<<endl;
         } 
          ranges[k][j]=atoi(range);
          flag2++;
          }

        k++;
        }
        linenum++;
     }
    
     for(i=0;i<flag1;i++)
     {
       cout<<"ranges[0]: "<<ranges[i][0]<<endl;
         cout<<"ranges[1]: "<<ranges[i][1]<<endl;
           cout<<"ranges[2]: "<<ranges[i][2]<<endl;
              cout<<"ranges[3]: "<<ranges[i][3]<<endl;
         cout<<"base[]: "<<classnames[i]<<endl;
     }
     cout<<")))"<<flag1<<endl;
     for(j=flag1;j<20;j++)
     {
       delete []classnames[j];  //释放多余的空间，delete []p表示释放指针P指向的空间
     }
 fin.close();
  return classnames;
}

char **search_baseclass(const char *filepath)  //遍历解析所有的文件AST-baseclass
{
     int i,j,k,l;    
     int linenum;
     int flag1=0,flag2=0;
     std::ifstream fin(filepath);
     std::string line;
     char **baseclass;
     baseclass=(char **)malloc(20*sizeof(char *));
	for (i=0;i<20;i++)
		baseclass[i]=(char *)malloc(50*sizeof(char));
     while (getline(fin, line)) {
     const char *cstr = line.c_str();
     char *pc = new char[100];
     strcpy(pc, cstr);
     int i,l; 
        if (pc[0] == '4'&& pc[1]=='4') {
        cout<<"class's linenum="<<linenum<<endl; //子类所在行数
          for(i=9;pc[i]!=',';i++)
          {
              baseclass[flag1][i-9]=pc[i];  //将“,”后边的类名称存入字符数组
          }
       flag1++;
       }
       linenum++;
       }
       
       for(i=0;i<flag1;i++)
       {
         cout<<"baseclass:   "<<baseclass[i]<<endl;
       }
  
      for(i=flag1;i<20;i++)
     {
       delete []baseclass[i];  //释放多余的空间，delete []p表示释放指针P指向的空间
     }
      
     
       fin.close();
      return baseclass;
}
char **search_classrange(const char *filepath)  //遍历解析所有的文件AST-classrange
{

}